home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5532 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  74 lines

  1. Path: zeus.rbi.informatik.uni-frankfurt.de!gna1pc
  2. From: Ferber@zoology.uni-frankfurt.de (Michael Ferber)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: write binary data to a file
  5. Date: Thu, 01 Feb 96 11:33:44 GMT
  6. Organization: Uni Frankfurt
  7. Message-ID: <4f54g8$sga@zeus.rbi.informatik.uni-frankfurt.de>
  8. NNTP-Posting-Host: gna1pc.zoologys.uni-frankfurt.de
  9. X-Newsreader: News Xpress Version 1.0 Beta #4
  10.  
  11. Return-Path: <alistair@articad.demon.co.uk>
  12. Delivery-Date: Mon, 5 Feb 1996 15:28:08 +0100
  13. Date: Mon, 5 Feb 1996 12:48:36 +0000
  14. To: Michael Ferber <Ferber@zoology.uni-frankfurt.de>
  15. From: Alistair Imrie <alistair@articad.demon.co.uk>
  16. Subject: Re: write binary data to a file
  17.  
  18. Dear Michael,
  19.  
  20. I have encountered exactly the problem you describe. Here is the
  21. solution I use:
  22.  
  23. Include the following filemcro.h file in any file where you want to
  24. write binary data:
  25.  
  26. /*BEGIN FILE*/
  27.  
  28. #if !defined __FILEMCRO_H
  29. #define __FILEMCRO_H
  30.  
  31. #include <fstream.h>
  32.  
  33. #define BinaryRead(item) read((char *)&item, sizeof(item))
  34. #define BinaryWrite(item) write((char *)&item, sizeof(item))
  35.  
  36. #endif // __FILEMCRO_H
  37.  
  38. /*END FILE*/
  39.  
  40. To use the macros, copy the technique used in this example:
  41.  
  42. void WriteTheseVariables(ostream& os, int var1, float var2, char var3)
  43. {
  44.     os.BinaryWrite(var1);
  45.     os.BinaryWrite(var2);
  46.     os.BinaryWrite(var3);
  47. }
  48.  
  49. void ReadTheseVariables(istream& is, int& var1, float& var2, char& var3)
  50. {
  51.     is.BinaryRead(var1);
  52.     is.BinaryRead(var2);
  53.     is.BinaryRead(var3);
  54. }
  55.  
  56. Hope this helps.
  57.  
  58. You are welcome to post this back to the newsgroup. I'm a bit thick and
  59. don't understand how to reply to news items, only how to email the
  60. author.
  61.   _   _
  62.  / | / \ '     |_  _  '  __
  63. |  | \ / | |\  |  / | | | 
  64.  \_|__X__|_/_|_|\_\_|_|_|
  65.  
  66. Alistair Imrie   EMail alistair@articad.demon.co.uk
  67.  
  68. ------------------------------------------------------------------------------------
  69. || Dr. Michael Ferber                     ||
  70. || Universitaet Frankfurt                 ||"science moves,
  71. || Zoologisches Institut                  || but slowly, slowly ......"
  72. || email: Ferber@zoology.uni-frankfurt.de ||                      Tennyson
  73. ------------------------------------------------------------------------------------
  74.